package com.xwj.back.utils;

import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;
import java.util.UUID;

public class FileUploadUtil {
    public static final String uploadPath = System.getProperty("user.dir") + "\\upload\\logo\\";

    public static String upload(MultipartFile file) throws IOException {
        //文件后缀，类型
        String fileSuffix = Objects.requireNonNull(file.getOriginalFilename()).substring(file.getOriginalFilename().lastIndexOf('.') + 1);
        //重命名文件名
        String fileName =  UUID.randomUUID().toString().replace("-","") + "." + fileSuffix;
        File fileDirectory = new File(uploadPath);
        if(!fileDirectory.exists()){
            fileDirectory.mkdirs();
        }
        //文件写入硬盘
        File descFile = new File(fileDirectory, fileName);
        file.transferTo(descFile);
        //文件URL
        return "/upload/" + fileName;
    }
}
